package com.tenglong.teacher.ui.setting.setting;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.clean.DataCleanManager;
import com.tenglong.teacher.R;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

public class AppCacheActivity extends BaseActivity {

    private TextView cleanTotal, homeworkSize, otherCacheSize, correctHomeworkSize, audioRecordSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_cache);
        initView();
        setListeners();
    }

    private void setListeners() {
        findViewById(R.id.homework).setOnClickListener(this);
        findViewById(R.id.other_cache).setOnClickListener(this);
        findViewById(R.id.correct_homework).setOnClickListener(this);
        findViewById(R.id.audioRecord).setOnClickListener(this);
        cleanTotal.setOnClickListener(this);
    }

    private void initView() {
        setTitle("清理缓存");
        cleanTotal = findViewById(R.id.complete);
        cleanTotal.setVisibility(View.VISIBLE);
        cleanTotal.setText("清理所有");

        homeworkSize = findViewById(R.id.homework_size);
        otherCacheSize = findViewById(R.id.other_cache_size);
        correctHomeworkSize = findViewById(R.id.correct_homework_size);
        audioRecordSize = findViewById(R.id.audioRecord_size);

        audioRecordSize.setText(DataCleanManager.getAudioRecorderSize(this));

        homeworkSize.setText(DataCleanManager.getPublishHomeworkSize(this));

        correctHomeworkSize.setText(DataCleanManager.getCorrectHomeworkSize(this));

        otherCacheSize.setText(DataCleanManager.getOtherCacheSize(this));
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.homework:
                if (DataCleanManager.getPublishHomeworkSize(this).equals("0.0MB")) {
                    ToastUtils.showCenterToast("没有可清理的");
                    return;
                }
                DataCleanManager.cleanPublishHomework(this);
                homeworkSize.setText(DataCleanManager.getPublishHomeworkSize(this));
                setResult(Activity.RESULT_OK);
                ToastUtils.showCenterToast("作业缓存已清理");
                break;
            case R.id.other_cache:
                if (DataCleanManager.getOtherCacheSize(this).equals("0.0MB")) {
                    ToastUtils.showCenterToast("没有可清理的");
                    return;
                }
                DataCleanManager.cleanOtherApplicationData(this);
                otherCacheSize.setText(DataCleanManager.getOtherCacheSize(this));
                setResult(Activity.RESULT_OK);
                ToastUtils.showCenterToast("其他缓存已清理");
                break;
            case R.id.correct_homework:
                if (DataCleanManager.getCorrectHomeworkSize(this).equals("0.0MB")) {
                    ToastUtils.showCenterToast("没有可清理的");
                    return;
                }
                DataCleanManager.cleanCorrectHomework(this);
                correctHomeworkSize.setText(DataCleanManager.getCorrectHomeworkSize(this));
                setResult(Activity.RESULT_OK);
                ToastUtils.showCenterToast("批改作业缓存已清理");
                break;
            case R.id.complete:
                if (DataCleanManager.getTotalCacheSize(this).equals("0.0MB")) {
                    ToastUtils.showCenterToast("没有可清理的");
                    return;
                }
                DataCleanManager.cleanApplicationData(this);
                correctHomeworkSize.setText(DataCleanManager.getCorrectHomeworkSize(this));
                homeworkSize.setText(DataCleanManager.getPublishHomeworkSize(this));
                otherCacheSize.setText(DataCleanManager.getOtherCacheSize(this));
                audioRecordSize.setText(DataCleanManager.getAudioRecorderSize(this));
                setResult(Activity.RESULT_OK);
                ToastUtils.showCenterToast("缓存已清理");
                break;
            case R.id.audioRecord:
                if (DataCleanManager.getAudioRecorderSize(this).equals("0.0MB")) {
                    ToastUtils.showCenterToast("没有可清理的");
                    return;
                }
                DataCleanManager.cleanAudioRecorder(this);
                audioRecordSize.setText(DataCleanManager.getAudioRecorderSize(this));
                setResult(Activity.RESULT_OK);
                ToastUtils.showCenterToast("语音缓存已清理");
                break;
        }
    }
}
